<template>
	<view class='goods'>
		<view class='goods-image'>
			<image :src="info.coverUrl" class='mainGooodsImage' mode=""></image>
		</view>
		<div class='goods-price'>
			<div class='left'>
				<div class='nowPrice'>{{info.payType==1||info.payType==6||this.info.payType==5?info.money:info.voucher}}
				</div>
				<div class='price' v-if='info.price'>¥{{info.price||0}}</div>
			</div>
			<!-- <div class='right'>已售：8799件</div> -->
		</div>
		<div class='title elliptical2'>
			<span class='newIcon'> 新品</span>
			{{info.title}}
		</div>
		<div class='splitLine'> </div>
		<div class="detail-title">宝贝详情</div>
		<div v-html="html"></div>

		<div class='options'>
			<!-- <div class='btn' @tap="goUrl">{{type==6?'立即购买':'立即兑换'}}</div> -->
			<div class='btn' @tap="choseGoods">{{type==6?'立即购买':'立即兑换'}}</div>
		</div>
		<uni-popup ref="popup" type="bottom">
			<view class="specification">
				<view class="goodTitle">
					<view class="titles">{{type==6?'商品购买':'商品兑换'}}</view>
					<image src="/static/goods/close.png" mode="" @tap="closeSpec"></image>
				</view>
				<view class="addressContent">
					<view class="addressLeft">
						<!-- 地址信息 -->
						<view class="addressInfo">
							<view class="addAddress"></view>
							<view v-if="addresssInfo.length != 0">
								<view class="phone"><span>{{addresssInfo.fullName}}</span>{{addresssInfo.mobile}}</view>
								<view class="address">
									{{addresssInfo.provinceText}}{{addresssInfo.cityText}}{{addresssInfo.areaText}}{{addresssInfo.address}}
								</view>
							</view>
							<!-- 如果没有地址 -->
							<view class='emptyAddress' v-else>
								<view class="addressTips">点击添加地址</view>
							</view>
						</view>
					</view>
					<!-- 右侧箭头 -->
					<image src="../../static/shopping/right.png" class="rightImg" @tap="goAddressList"></image>
				</view>
				<view class="good-content">
					<view class="left">
						<image :src="coverImg" mode=""></image>
					</view>
					<view class="right">
						<view class="goodTitle">{{info.title}}</view>
						<view class="number">
							<view class='label'>数量</view>
							<view class='value'>
								<view class='counter'>
									<view class='subtraction' @click="handleCount(1)">
										<image src="/static/goods/prev.png" mode=""></image>
									</view>
									<input class='num' type="number" @input="valueFormat" v-model="orderInfo.goodsNum"> </input>
									<view class='add' @click='handleCount(2)'>
										<image src='/static/goods/add.png' mode=""></image>
									</view>
								</view>
							</view>
						</view>
						<view class="price"> <text class="single">单件</text> <text>¥</text>{{orderInfo.goodsPrice}}</view>
					</view>
				</view>
				<view class="choose">
					<view class="specTitle">规格</view>
					<view class="specList">
						<view :class="index === specIndex ? 'activeSpecItem':'specItem'" v-for="(item,index) in info.skuList" :key="index" @tap="choseSpec(item,index)">{{item.specName}}</view> 
					</view>
				</view>
				<view class="goPay" @tap="goUrl">{{type==6?'立即购买':'立即兑换'}}</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		buyGoods,
		getVoucherGoods,
		appointment,
		addressList
	} from '../../api/api.js'
	export default {
		name: 'goods',
		data() {
			return {
				goodsId: '',
				isVoucher: '',
				isVip: 0, // 是否是会员礼包商品 1 是 0 不是
				// 地址列表
				addressList: [],
				// 地址信息
				addresssInfo: '',
				// 地址id
				addressId: '',
				info: {},
				navId: '',
				bugPrice: 0,
				html: '',
				type: '',
				orderInfo:{},
				specIndex:0,
				coverImg:'',//展示图
			}
		},
		mounted() {
			// 获取商品详情
			this.getDetail()
		},
		onShow() {
			// 获取地址列表
			this.getAddressList()
		},
		onLoad(e) {
			this.goodsId = e.id
			this.isVoucher = e.isVoucher
			this.isVip = e.isVip
			this.navId = e.navId
			this.source = e.source
			this.type = e.type
			
		},
		methods: {
			

			// 获取商品详情
			getDetail() {
				let data = {
					id: this.goodsId
				}
				getVoucherGoods(data).then(res => {
					this.info = res.data
					const skuList = this.info.skuList
					this.specIndex = skuList.findIndex(item => item.isDefault == 1)
					this.coverImg = skuList[this.specIndex].imageUrl || this.info.coverUrl
					this.orderInfo = {
						goodsNum: 1,
						goodsImg: this.info.coverUrl,
						goodsId: this.info.id,
						goodsTitle: this.info.title,
						skuId : skuList[this.specIndex].skuId,
						goodsPrice: this.info.payType == 1 || this.info.payType == 6 || this.info.payType == 5 ? this.info
							.money : this.info.voucher,
					}
					var regex = new RegExp('<img ', 'gi')
					this.html = this.info.detail.replace(regex, '<img style="width: 100%;" ');
					this.price = this.navId == 1 ? Number(this.info.money) : Number(this.info.voucher)
					this.buyPrice = this.price
				})
			},
			//获取用户地址
			getAddressList() {
				let data = {}
				addressList(data).then(res => {
					this.addressList = res.data
					// 如果没有添加地址,提示去添加
					if (this.addressList.length == 0) {
						this.addresssInfo = ''
					} else if (this.addressList.length == 1) {
						// 如果只有一个地址,直接显示为默认
						this.addresssInfo = this.addressList[0]
						this.addressId = this.addressList[0].id
					} else if (this.addressList.length > 1) {
						// 如果存在多个地址,先判断是否有默认地址
						let isHaveDefault = this.addressList.some(item => item.isDefault == 1)
						// 如果有,查询默认地址
						if (isHaveDefault) {
							this.addressList.forEach(item => {
								if (item.isDefault == 1) {
									this.addresssInfo = item
									this.addressId = item.id
								}
							})
						} else {
							// 如果没有,默认第一条地址
							this.addresssInfo = this.addressList[0]
							this.addressId = this.addressList[0].id
						}
					}
				})
			},
			// 前往地址列表
			goAddressList() {
				uni.navigateTo({
					url: './addressList?isSubmit=1'
				})
			},
			choseSpec(item,index){
				this.specIndex = index
				this.orderInfo.goodsPrice = item.price
				this.orderInfo.skuId = item.skuId
				this.coverImg = this.info.skuList[index].imageUrl
			},
			valueFormat(event) {
				const value = event.target.value;
				if (value === '' || /^[1-9]\d*$/.test(value)) {
					this.orderInfo.goodsNum = value;
				} else {
					// 如果不符合条件，可以设置一个默认值或者不更新数据模型
					this.orderInfo.goodsNum = ''; // 清空输入或设置为上一个有效值
				}
			},
			goUrl() {
				if(this.specIndex === ''){
					uni.showToast({
						icon:'none',
						title:'请选择商品规格'
					})
					return
				}
				console.log();
				if(this.orderInfo.goodsNum < 0){
					uni.showToast({
						icon:'none',
						title:'请选择正确的数量'
					})
					return
				}
				const orderInfo = JSON.stringify(this.orderInfo)
				console.log(orderInfo);
				uni.navigateTo({
					url: '../goods/submitOrder?orderInfo=' + orderInfo + '&payType=' + this.info.payType +
						'&isVip=' + this.info.isVip + "&source=" + this.source +"&goodsSkuId=" + this.orderInfo.skuId
				})
			},
			handleCount(type) {
				if (type == 1) {
					if (this.orderInfo.goodsNum <= 1) {
						uni.showToast({
							icon:'none',
							title:'数量不能小于1'
						})
						return
					}
					this.orderInfo.goodsNum--
				}
				if (type == 2) {
					this.orderInfo.goodsNum++
				}
			},
			choseGoods() {
				this.$refs.popup.open('bottom')
			},
			closeSpec(){
				this.$refs.popup.close()
			}
		}
	}
</script>

<style lang="less" scoped>
	.goods {
		display: flex;
		flex-direction: column;
		padding-bottom: 108rpx;
	}

	.goods-image {
		width: 100vw;
		height: 750rpx;


	}

	.mainGooodsImage {
		width: 100vw;
		height: 750rpx;
	}

	.goods-price {
		width: 100%;
		height: 80rpx;
		padding: 24rpx 20rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.goods-price .left {
		display: flex;
		align-items: center;
	}

	.goods-price .left .nowPrice {
		font-size: 40rpx;
		color: #FF3C2D;
	}

	.goods-price .left .price {
		font-weight: 400;
		font-size: 20rpx;
		color: #C8C8C8;
		margin-left: 8rpx;
		text-decoration: line-through;

	}

	.goods-price .left .nowPrice:before {
		content: '¥';
		font-size: 20rpx;
		color: #FF3C2D;
		margin-right: 4rpx;
	}

	.goods-price .right {
		font-weight: 400;
		font-size: 30rpx;
		color: #C8C8C8;
	}

	.title {
		padding: 0 20rpx;
		box-sizing: border-box;
		font-size: 30rpx;
		line-height: 40rpx;
		margin-bottom: 28rpx;
	}

	.title .newIcon {
		padding: 2rpx 4rpx;
		background: #FF3C2D;
		border-radius: 4rpx;
		font-weight: 800;
		font-size: 24rpx;
		color: #FFFFFF;
		margin-right: 8rpx;
	}

	.splitLine {
		width: 100%;
		height: 20rpx;
		background: #F7F7F7;
	}

	.detail-title {
		width: 100%;
		height: 88rpx;
		background: #FFFFFF;
		font-weight: bold;
		font-size: 36rpx;
		color: #3C3C3C;
		display: flex;
		align-items: center;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.options {
		width: 100%;
		position: fixed;
		bottom: 0;
		height: 108rpx;
		background: #FFFFFF;
		box-shadow: 0px -6rpx 8rpx 0px rgba(199, 199, 199, 0.25);
		background: #FFFFFF;
		border-top-right-radius: 30rpx;
		border-top-left-radius: 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;

	}

	.options .btn {
		width: 670rpx;
		height: 88rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-weight: bold;
		font-size: 32rpx;
		color: #FFFFFF;
		border-radius: 88rpx;
		margin: 0 auto;
		background-color: #00C538;
	}

	.specification {
		width: calc(100% - 60rpx);
		background-color: #fff;
		border-radius: 30rpx 30rpx 0 0;
		padding: 29rpx 30rpx;

		.goodTitle {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.titles {
				font-weight: 500;
				font-size: 34rpx;
				color: #181A1C;
			}

			image {
				width: 42rpx;
				height: 42rpx;
			}
		}

		.addressContent {
			width: 100%;
			padding: 30rpx 20rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #FFFFFF;
			border-bottom: 1rpx solid #EAEAEA;

			.addressLeft {
				.addressInfo {
					display: flex;
					align-items: center;

					.phone {
						font-size: 28rpx;
						font-family: PingFangSC-Bold, PingFang SC;
						font-weight: bold;
						color: #1A1A1A;

						span {
							margin-right: 24rpx;
						}
					}

					.address {
						margin-top: 20rpx;
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #1A1A1A;
					}

					.emptyAddress {
						display: flex;
						align-items: center;
					}
				}
			}

			.addAddress {
				width: 48rpx;
				height: 48rpx;
				background: url('@/static/statc-icon/image-4.png') no-repeat;
				background-size: 100% 100%;
				margin-right: 20rpx;
			}

			.rightImg {
				width: 20rpx;
				height: 30rpx;
			}

		}
		.good-content{
			display: flex;
			margin-top: 40rpx;
			.left{
				image{
					width: 210rpx;
					height: 210rpx;
					border-radius: 20rpx;
				}
			}
			.right{
				margin-left: 14rpx;
				.goodTitle{
					font-weight: 400;
					font-size: 28rpx;
					color: #181A1C;
				}
				.number{
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 24rpx 0;
					box-sizing: border-box;
					.label {
						font-weight: 400;
						font-size: 26rpx;
						color: #898A8B;
						transform: translateY(-3rpx);
					}
					.counter {
						display: flex;
						align-items: center;
						margin-left: 20rpx;
						.subtraction {
							image{
								width: 48rpx;
								height: 48rpx;
							}
						}
						.num {
							width: 100rpx;
							font-weight: bold;
							font-size: 28rpx;
							color: #000000;
							text-align: center;
						}
						.add {
							image{
								width: 48rpx;
								height: 48rpx;
							}
						
						}
					}
				}
				.price{
					font-weight: 500;
					font-size: 42rpx;
					color: #FD413A;
					.single{
						margin-right: 10rpx;
					}
					text{
						font-weight: 400;
						font-size: 22rpx;
						color: #FD413A;
					}
				}
			}
		}
		.choose{
			margin-top: 40rpx;
			.specTitle{
				font-weight: 500;
				font-size: 30rpx;
				color: #333333;
			}
			.specList{
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				.activeSpecItem{
					margin-top: 20rpx;
					margin-right: 16rpx;
					padding: 10rpx 40rpx;
					background: #EAF8EB;
					border-radius: 12rpx;
					font-weight: 400;
					font-size: 26rpx;
					color: #29C500;
					border: 2rpx solid #29C500;
				}
				.specItem{
					margin-top: 20rpx;
					margin-right: 16rpx;
					padding: 10rpx 40rpx;
					background: #F7F7F7;
					border-radius: 12rpx;
					font-weight: 400;
					font-size: 26rpx;
					color: #333333;
					line-height: 37rp
				}
			}
		}
		.goPay{
			width: 100%;
			height: 90rpx;
			background: #2DD800;
			border-radius:50rpx;
			text-align: center;
			line-height: 90rpx;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			margin-top: 56rpx;
		}
	}
</style>